/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.db_vdumper.ui.component.db;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.ActionListener;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import org.opu.db_vdumper.beans.UserData;

/**
 *
 * @author yura
 */
public class ConnectionDialog extends JDialog{

    private Action[] acions;
    private JButton[] submitBtn;

    private ConnectionDialogI18n i18n;
    
    private UserData dbData;
    private UserDataPane dbDataPane;

    public ConnectionDialog(UserData userData, Action... submit){

        this(null, null, true, userData, new DefaultConnectionDialogI18n(), submit);

        setTitle(i18n.getTitle());
    }

    public ConnectionDialog(Frame owner, String title, boolean modal,
            UserData userData, Action... submit){

        this(owner, title, modal, userData, new DefaultConnectionDialogI18n(), submit);
    }

    public ConnectionDialog(Frame owner, String title, boolean modal, 
            UserData userData, ConnectionDialogI18n i18n, Action... submit) {

        super(owner, title, modal);

        this.acions = submit;
        this.dbData = userData;
        this.i18n = i18n;

        initComponent();
    }

    public void reinit() {
        removeAll();
        initComponent();
    }

    private void initComponent() {
        dbDataPane = new UserDataPane(dbData);
        submitBtn = new JButton[acions.length];
        for (int i = 0; i < acions.length; i++) {
            submitBtn[i] = new JButton(acions[i]);
        }

        Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalGlue());
        for (int i = 0; i < acions.length; i++) {
            box.add(submitBtn[i]);
            box.add(Box.createHorizontalStrut(16));
        }

        Container content = getContentPane();
        content.add(dbDataPane);
        content.add(box, BorderLayout.SOUTH);

        pack();
    }


    public void addApproveAction(ActionListener action){
        dbDataPane.addApproveAction(action);
    }

    public UserData getDbData() {
        return dbDataPane.getData();
    }

    public ConnectionDialogI18n getI18n() {
        return i18n;
    }

    public UserDataPane getDbDataPane() {
        return dbDataPane;
    }

}
